import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/stores_requisition_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/stores_requisition.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';

class StoresRequisitionEdit extends StatefulWidget {
  StoresRequisitionEdit({this.storesRequisition});

  final StoresRequisition? storesRequisition;

  @override
  _StoresRequisitionEditState createState() => _StoresRequisitionEditState();
}

class _StoresRequisitionEditState extends State<StoresRequisitionEdit> {
  late StoresRequisition storesRequisition;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  DateFormat format = DateFormat('yyyy-MM-dd');
  List<SelectOptionVO> workshopIdList = [];

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.storesRequisition == null;
    this.storesRequisition = widget.storesRequisition ?? StoresRequisition();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    // this.workshopIdList = await DynamicDataApi.getModelSelect(
    //     // active: false,
    //     model: "hr.department",
    //     name: "complete_name",
    //     key: 'parent_id',
    //     operator: "!=",
    //     val: null);
    storesRequisition.userId = userInfo.userId;
    storesRequisition.userName = userInfo.name;
    if (this.isAdd!) {
      storesRequisition.workshopId = userInfo.deptId;
      storesRequisition.workshopName = userInfo.deptName;
      storesRequisition.requisitionDate = Utils.getDate();
      ResponseBodyApi res = await StoresRequisitionOdooApi.getNo();
      if(res.success!){
        storesRequisition.number = res.data;
      }
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '领料单号',
            enable: false,
            value: storesRequisition.number,
            onSaved: (v) {
              storesRequisition.number = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '用途',
            value: storesRequisition.purpose,
            onSaved: (v) {
              storesRequisition.purpose = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '生产车间',
            enable: false,
            value: storesRequisition.workshopName,
            onSaved: (v) {
              storesRequisition.workshopName = v;
            },
          ),
          // CrySelect(
          //   label: '生产车间',
          //   enable: false,
          //   dataList: workshopIdList,
          //   value: storesRequisition.workshopId,
          //   onSaved: (v) => {storesRequisition.workshopId = v},
          //   onChange: (v) async {
          //     if (storesRequisition.workshopId != v) {
          //       formKey.currentState!.save();
          //       storesRequisition.workshopId = v;
          //       setState(() {});
          //     }
          //   },
          // ),
          CryInput(
            label: '领料人',
            enable: false,
            value: storesRequisition.userName,
            onSaved: (v) {
              storesRequisition.userName = v;
            },
          ),
          CrySelectDate(
            context,
            label: '领料日期',
            value: storesRequisition.requisitionDate,
            onDateChange: (v) {
              formKey.currentState?.save();
              storesRequisition.requisitionDate = v;
              setState(() {});
            },
            onSaved: (v) {
              storesRequisition.requisitionDate = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            StoresRequisitionOdooApi.saveOrUpdate(storesRequisition.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
